//
//  MainViewController.m
//  yinsiwenzhen
//
//  Created by Yangfan on 14/12/24.
//  Copyright (c) 2014年 4gread. All rights reserved.
//

#import "MainViewController.h"
#import "ListViewController.h"
#import "UMFeedback.h"
#import "IdeaWithUsViewController.h"
@interface MainViewController ()

@end

@implementation MainViewController


- (void)viewDidLoad {
    [super viewDidLoad];

    [self UI];
   if (!connectionNetFlag) {// 没有连接网络
       [self showAlertView];
   }else{
       BOOL check=[[NSUserDefaults standardUserDefaults]boolForKey:@"first"];
       if (check==NO) {
//           NSLog(@"该行代码只执行一次");
           [self  getData];
           [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"first"];
           [NSUserDefaults resetStandardUserDefaults];
       }
   }
}
-(void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];
}
- (void)UI {
    
   self.navigationController.navigationBar.tintColor = HexRGB(0x2AADEA);
    
    UIScrollView *mainScroll = [[UIScrollView alloc] init];
    [mainScroll setFrame:CGRectMake(0, 64, Kwidth, Kheight - 64)];
    [mainScroll setBackgroundColor:[UIColor whiteColor]];
    [mainScroll setShowsHorizontalScrollIndicator:NO];
    [mainScroll setShowsVerticalScrollIndicator:NO];
    [self.view addSubview:mainScroll];
    
    // 上面的Logo图片
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"Logo"];
    [mainScroll addSubview:imageView];
    
    // 开始问诊按钮
    UIButton *starAskingButton = [[UIButton alloc] init];
    starAskingButton.tag = 51;
    [starAskingButton addTarget:self action:@selector(allAction:) forControlEvents:UIControlEventTouchUpInside];
    [starAskingButton setBackgroundImage:[UIImage imageNamed:@"starAsking"] forState:UIControlStateNormal];
    [starAskingButton setBackgroundImage:[UIImage imageNamed:@"starAsking"] forState:UIControlStateHighlighted];
    [mainScroll addSubview:starAskingButton];
    
    // 问诊记录
    UIButton *askingHistoryButton = [[UIButton alloc] init];
    askingHistoryButton.tag = 50;
    [askingHistoryButton addTarget:self action:@selector(allAction:) forControlEvents:UIControlEventTouchUpInside];
    [askingHistoryButton setBackgroundImage:[UIImage imageNamed:@"wenzhengjilu"] forState:UIControlStateNormal];
    [askingHistoryButton setBackgroundImage:[UIImage imageNamed:@"wenzhengjilu"] forState:UIControlStateHighlighted];
    [mainScroll addSubview:askingHistoryButton];
    

    
    NSLog(@"****%f", Kwidth);
    if (isiphone6plus) {
        [imageView setFrame:CGRectMake(95, 50, Kwidth - 95 * 2, (Kwidth - 95 * 2)* 200.0 / 225.0)];
//        [askingHistoryButton setFrame:CGRectMake(25, imageView.frame.origin.y + imageView.frame.size.height + 34, Kwidth - 25 * 2, (Kwidth - 25 * 2) * 66.0 / 363.0)];
        [starAskingButton setFrame:CGRectMake(25, imageView.frame.origin.y + imageView.frame.size.height + 40, Kwidth - 25 * 2, (Kwidth - 25 * 2) * 66.0 / 362.0)];
        [askingHistoryButton setFrame:CGRectMake(25, starAskingButton.frame.origin.y + starAskingButton.frame.size.height + 20, Kwidth - 25 * 2, (Kwidth - 25 * 2) * 66.0 / 363.0)];
    } else {
        if (isiphone4) {
            [imageView setFrame:CGRectMake(76, 18, Kwidth - 76 * 2, (Kwidth - 76 * 2)* 200.0 / 225.0)];
            [starAskingButton setFrame:CGRectMake(15, imageView.frame.origin.y + imageView.frame.size.height + 17, Kwidth - 15 * 2, (Kwidth - 15 * 2) * 66.0 / 362.0)];
            [askingHistoryButton setFrame:CGRectMake(15, starAskingButton.frame.origin.y + starAskingButton.frame.size.height + 12, Kwidth - 15 * 2, (Kwidth - 15 * 2) * 66.0 / 363.0)];
        } else {
            [imageView setFrame:CGRectMake(76, 40, Kwidth - 76 * 2, (Kwidth - 76 * 2)* 200.0 / 225.0)];
            [starAskingButton setFrame:CGRectMake(15, imageView.frame.origin.y + imageView.frame.size.height + 39, Kwidth - 15 * 2, (Kwidth - 15 * 2) * 66.0 / 362.0)];
            [askingHistoryButton setFrame:CGRectMake(15, starAskingButton.frame.origin.y + starAskingButton.frame.size.height + 14, Kwidth - 15 * 2, (Kwidth - 15 * 2) * 66.0 / 363.0)];
        }
    }
    
    // 下面的“关于我们”三个按钮
    UIButton *otherThreeButton;
    for (int i = 0; i < 3; i++) {
        otherThreeButton = [[UIButton alloc] init];
        otherThreeButton.tag = 52 + i;
        [otherThreeButton addTarget:self action:@selector(allAction:) forControlEvents:UIControlEventTouchUpInside];
        switch (i) {
            case 0:
            {
                
                [otherThreeButton setBackgroundImage:[UIImage imageNamed:@"aboutUs"] forState:UIControlStateNormal];
                [otherThreeButton setBackgroundImage:[UIImage imageNamed:@"aboutUs"] forState:UIControlStateHighlighted];
            }
                break;
            case 1:
            {
                [otherThreeButton setBackgroundImage:[UIImage imageNamed:@"yinsibaohu"] forState:UIControlStateNormal];
                [otherThreeButton setBackgroundImage:[UIImage imageNamed:@"yinsibaohu"] forState:UIControlStateHighlighted];
            }
                break;
            case 2:
            {
                [otherThreeButton setBackgroundImage:[UIImage imageNamed:@"yijianfankui"] forState:UIControlStateNormal];
                [otherThreeButton setBackgroundImage:[UIImage imageNamed:@"yijianfankui"] forState:UIControlStateHighlighted];
            }
                break;

                
            default:
                break;
        }
        [mainScroll addSubview:otherThreeButton];
        
        if (isiphone6plus) {
            [otherThreeButton setFrame:CGRectMake(25 + (113 + 12) * i, askingHistoryButton.frame.origin.y + askingHistoryButton.frame.size.height + 20, 113, 114)];
        } else {
            [otherThreeButton setFrame:CGRectMake(15 + ((Kwidth - 50) / 3.0 + 10) * i, askingHistoryButton.frame.origin.y + askingHistoryButton.frame.size.height + 12, (Kwidth - 50) / 3.0, (Kwidth - 50) / 3.0)];
        }
    }
    
    
    if (isiphone4) {
        [mainScroll setContentSize:CGSizeMake(Kwidth, otherThreeButton.frame.origin.y + otherThreeButton.frame.size.height + 10)];
    } else {
        [mainScroll setContentSize:CGSizeMake(Kwidth, otherThreeButton.frame.origin.y + otherThreeButton.frame.size.height + 20)];
    }
}

- (void)allAction:(UIButton *)sender {
    _ideaView = [[IdeaWithUsViewController alloc] init];
    switch (sender.tag) {
        case 50:{// 问诊记录
            ListViewController *list = [[ListViewController alloc] init];
            
            [self.navigationController pushViewController:list animated:YES];
        }
            break;
        case 51:{// 开始问诊按钮
            [self performSegueWithIdentifier:@"staroneToFive" sender:self];
        }
            
            break;
        case 52:{//关于我们
            _ideaView.tag = sender.tag;
            [self.navigationController pushViewController:_ideaView animated:YES];
        }
            break;
        case 53:{// 隐私保护
            _ideaView.tag = sender.tag;
            [self.navigationController pushViewController:_ideaView animated:YES];
        }
            break;
        case 54:{// 意见反馈
            [self.navigationController pushViewController:[UMFeedback feedbackViewController]
                                                 animated:YES];
        }
            break;
            
        default:
            break;
    }
}

-(void)getData{
    
    _userDefaultes = [NSUserDefaults standardUserDefaults];
    _objectIdArr = [[NSMutableArray alloc] initWithCapacity:0];
    
    AVQuery *query = [AVQuery queryWithClassName:@"equipmentId"];
    NSArray *allArr = [query findObjects];
    if (allArr.count == 0) {
        [self saveDate];
    }else{
        
        for (int i = 0 ; i < allArr.count; i++) {
            AVObject *List = [allArr objectAtIndex:i];
            [_objectIdArr addObject:List.objectId];// 获取所有的objectId
            
        }
        NSString *myString = [_userDefaultes stringForKey:@"personage"];// 获取本地数据
        NSLog(@"------获取本地存储的数据是-----%@",myString);
        if ([_objectIdArr containsObject:myString]) {
            
        }else{
            [self saveDate];
        }
    }
    
}
// 保存用户的数据
-(void)saveDate{
    _userDefaultes = [NSUserDefaults standardUserDefaults];
    
    AVObject *strObject = [AVObject objectWithClassName:@"equipmentId"];
    [strObject setObject:@"2" forKey:@"user"];
    [strObject save];
    [strObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        _idString = strObject.objectId;
        NSLog(@"------保存的ID是------%@",_idString);
    }];
    [_userDefaultes setObject:_idString forKey:@"personage"];// 保存到本地
    [_userDefaultes synchronize];
    
//    NSString *myString = [_userDefaultes stringForKey:@"personage"];// 获取本地数据
//    NSLog(@"------获取本地存储的数据是-----%@",myString);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
